#/home/xingjun/database/code/singleCell/findTopGene.R

args=commandArgs(T)
sampleName <- args[1]
workDir <- args[2]
RData <- args[3]

#output
#x.ClusMark.RData
#x.ClusMark.xls
#100 genes

findTopGene <- function(sampleName,workDir,RData)
{
    library(Seurat)
    setwd(workDir)
    load(RData)
    print("find Top Gene:")
    Allcell.markers <- Seurat::FindAllMarkers(object = all10x.Seurat, only.pos = TRUE, min.pct = 0.25, thresh.use = 0.25)
    print("save result as RData:")
    TopGeneRData = paste(sampleName,".ClusMark.RData",sep = "")
    save(Allcell.markers,file=TopGeneRData)
    print(TopGeneRData)

    Allcell.markers.top50=lapply(as.vector(unique(Allcell.markers$cluster)),function(x)
    {
        top50=Allcell.markers[which(Allcell.markers$cluster==x),"gene"][1:100]
    })

    temp <- as.data.frame(Allcell.markers.top50[1])
    colnames(temp) <- "0"
    for(v in seq(2,length(Allcell.markers.top50),1))
    {
        temp1 <- as.data.frame(Allcell.markers.top50[v])
        colnames(temp1) <- v - 1
        temp <- cbind(temp,temp1)
    }

    print("save every cluster Top Gene:")
    TopGeneTable = paste(sampleName,".ClusMark.xls",sep = "")
    write.table(temp,TopGeneTable,quote=F,sep="\t")
    print(TopGeneTable)
}

findTopGene(sampleName,workDir,RData)
